﻿#region Copyright (c) 2006-2012 nHydrate.org, All Rights Reserved
// --------------------------------------------------------------------- *
//                           NHYDRATE.ORG                                *
//              Copyright (c) 2006-2012 All Rights reserved              *
//                                                                       *
//                                                                       *
// This file and its contents are protected by United States and         *
// International copyright laws.  Unauthorized reproduction and/or       *
// distribution of all or any portion of the code contained herein       *
// is strictly prohibited and will result in severe civil and criminal   *
// penalties.  Any violations of this copyright will be prosecuted       *
// to the fullest extent possible under law.                             *
//                                                                       *
// THE SOURCE CODE CONTAINED HEREIN AND IN RELATED FILES IS PROVIDED     *
// TO THE REGISTERED DEVELOPER FOR THE PURPOSES OF EDUCATION AND         *
// TROUBLESHOOTING. UNDER NO CIRCUMSTANCES MAY ANY PORTION OF THE SOURCE *
// CODE BE DISTRIBUTED, DISCLOSED OR OTHERWISE MADE AVAILABLE TO ANY     *
// THIRD PARTY WITHOUT THE EXPRESS WRITTEN CONSENT OF THE NHYDRATE GROUP *
//                                                                       *
// UNDER NO CIRCUMSTANCES MAY THE SOURCE CODE BE USED IN WHOLE OR IN     *
// PART, AS THE BASIS FOR CREATING A PRODUCT THAT PROVIDES THE SAME, OR  *
// SUBSTANTIALLY THE SAME, FUNCTIONALITY AS THIS PRODUCT                 *
//                                                                       *
// THE REGISTERED DEVELOPER ACKNOWLEDGES THAT THIS SOURCE CODE           *
// CONTAINS VALUABLE AND PROPRIETARY TRADE SECRETS OF NHYDRATE,          *
// THE REGISTERED DEVELOPER AGREES TO EXPEND EVERY EFFORT TO             *
// INSURE ITS CONFIDENTIALITY.                                           *
//                                                                       *
// THE END USER LICENSE AGREEMENT (EULA) ACCOMPANYING THE PRODUCT        *
// PERMITS THE REGISTERED DEVELOPER TO REDISTRIBUTE THE PRODUCT IN       *
// EXECUTABLE FORM ONLY IN SUPPORT OF APPLICATIONS WRITTEN USING         *
// THE PRODUCT.  IT DOES NOT PROVIDE ANY RIGHTS REGARDING THE            *
// SOURCE CODE CONTAINED HEREIN.                                         *
//                                                                       *
// THIS COPYRIGHT NOTICE MAY NOT BE REMOVED FROM THIS FILE.              *
// --------------------------------------------------------------------- *
#endregion
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace nHydrate.DslPackage.Forms
{
	public partial class RunningProcesses : Form
	{
		private const string WINDOW_TEXT = "Processing nHydrate Model";
		private bool _hasShown = false;
		private DateTime _loaded = DateTime.Now;

		public RunningProcesses()
		{
			InitializeComponent();
			this.Text = WINDOW_TEXT;
			lblText.Text = string.Empty;
			lblSubText.Text = string.Empty;
			timer1.Start();
		}

		public void UpdateSubText(string text, string subText)
		{
			UpdateSubText(text, subText, -1, string.Empty);
		}

		public void UpdateSubText(string text, string subText, int progress, string windowTextAppend)
		{
			if (!string.IsNullOrEmpty(text))
			{
				lblText.Text = text;
			}

			lblSubText.Text = TruncatePathForDisplay(subText, lblSubText.Width, lblSubText.Font, TextFormatFlags.PathEllipsis);
			if (!string.IsNullOrEmpty(windowTextAppend))
			{
				this.Text = WINDOW_TEXT + " " + windowTextAppend;
			}

			if (0 <= progress && progress <= 100)
			{
				if (timer1.Enabled)
				{
					timer1.Stop();
					timer1.Enabled = false;
				}
				progressBar1.Value = progress;
			}
		}

		public void Update(string text, bool topMost, int timeout)
		{
			lblText.Text = text;
			this.TopMost = topMost;
			var tick = (timeout * 1000) / 100;
			if (tick <= 0) tick = 100;
			timer1.Interval = tick;
			this.Activate();
		}

		private void timer1_Tick(object sender, EventArgs e)
		{
			var v = progressBar1.Value + 1;
			if (v > 100) v = 100;
			progressBar1.Value = v;
			Application.DoEvents();

			//Wait a short time and pop on top just in case it is hidden
			if (!_hasShown && DateTime.Now.Subtract(_loaded).TotalMilliseconds > 1000)
			{
				_hasShown = true;
				this.Activate();
			}
		}

		private static string TruncatePathForDisplay(string text, int Width, System.Drawing.Font font, System.Windows.Forms.TextFormatFlags FormatFlags)
		{
			var result = string.Copy(text);
			TextRenderer.MeasureText(result, font, new System.Drawing.Size(Width, 0), FormatFlags | TextFormatFlags.ModifyString);
			return result;
		}

	}
}